<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 认证类，加入一些权限认证等方法
 * @author Administrator
 *
 */
class Auth{
    var $user = null;
    //专营店常量
    var $roleSellpoint = 1;
    //小区督导端常量
    var $roleSpsarea = 2;
    //大区总监常量
    var $roleRegion = 3;
    //区域总监常量
    var $roleArea  = 4;
    //总部常量
    var $roleHead = 5;
    //查看角色常量
    var $roleCheck = 6;
    //时段模型
    var $timeUnitModel = null;
    //补报角色模型
    var $supplementRoleModel = null;
    //补报审核模型
    var $supplementAuditModel = null;
    //用户类
    var $userLib = null;
    /**
     * 构造函数
     * @param array $user
     */
    function Auth($user){
        $this->user = $user;
        //自定义类使用CI资源使用此方法
        $CI =& get_instance();
        $CI->load->model('timeUnitModel');
        $this->timeUnitModel = $CI->timeUnitModel;
        //补报角色模型
        $CI->load->model('supplementRoleModel');
        $this->supplementRoleModel = $CI->supplementRoleModel;
        //补报审核模型
        $CI->load->model('supplementAuditModel');
        $this->supplementAuditModel = $CI->supplementAuditModel;
        //用户类
        $CI->load->library('User', null,'userLib');
        $this->userLib = $CI->userLib;
    }
    /**
     * 是否有添加预算申请权限
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetAdd($procData){
        if ($procData['id']){
            return false;
        }
        //年份验证
        if (!isset($procData['year']) || !$procData['year']){
            return false;
        }
        //时间验证
        if (!isset($procData['timeUnit']) || !$procData['timeUnit']){
            return false;
        }
        //用户身份验证
        if ($this->user['userRole'] != $this->roleSellpoint || !array_intersect(array('apply','100CityApply'), $this->user['userLimit'])){
            return false;
        }
        //是否为特殊补报
        if ($this->isSingleSpecialBubao($procData)){
            return true;
        }
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (!isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            return false;
        }else{
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
        }
        $nowTime = time();//当前时间戳在预算申请开始和结束时间之内
        if ($nowTime < $timeUnitData['singleBudgetApplyStartTime'] || $nowTime > $timeUnitData['singleBudgetApplyEndTime'] ){
            return false;
        }
        return true;
    }
    /**
     * 是否有修改预算申请权限
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetEdit($procData){
        //年份验证
        if (!isset($procData['year']) || !$procData['year']){
            return false;
        }
        //时间验证
        if (!isset($procData['timeUnit']) || !$procData['timeUnit']){
            return false;
        }
        //用户身份验证
        if ($this->user['userRole'] != $this->roleSellpoint || !array_intersect(array('apply','100CityApply'), $this->user['userLimit'])){
            return false;
        }
        if($procData['storeId'] != $this->user['userId']){
            return false;
        }
        //数据有效性验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //是否为特殊补报
        if ($this->isSingleSpecialBubao($procData)){
            return true;
        }
        //时间段缓存数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (!isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            return false;
        }else{
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
        }
        $nowTime = time();//当前时间戳在预算申请开始和结束时间之内
        if ($nowTime < $timeUnitData['singleBudgetApplyStartTime'] || $nowTime > $timeUnitData['singleBudgetApplyEndTime'] ){
            return false;
        }
        return true;
    }
    /**
     * 是否有查看单店预算权限;
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetCheck($procData){
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        return true;
    }
    /**
     * 判断用户是否存在预算审核权限
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetAudit($procData){
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //只有督导、大区总监才有审核权限
        if (!array_intersect(array('spsarea','region','area'), $this->user['userLimit'])){
            return false;
        }
        //判断是否在管理范围内
        if (!in_array($procData['storeId'], $this->userLib->getViewStoreIds())){
            return false;
        }
        //是否为特殊补报
        if ($this->isSingleSpecialBubao($procData)){
            return true;
        }
        //时间段缓存数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (!isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            return false;
        }else{
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
        }
        //当前时间
        $nowTime = time();
        if ($nowTime < $timeUnitData['singleBudgetAuditStartTime'] || $nowTime > $timeUnitData['singleBudgetAuditEndTime'] ){
            return false;
        }
        return true;
    }
    /**
     * 结算申请权限
     * @param array $procData
     * @return boolean
     */
    function isSingleSettleEdit($procData){
        //时间验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //用户身份验证
        if ($this->user['userRole'] != $this->roleSellpoint || !array_intersect(array('apply','100CityApply'), $this->user['userLimit'])){
            return false;
        }
        if($procData['storeId'] != $this->user['userId']){
            return false;
        }
        //数据有效性验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //是否为特殊补报
        if ($this->isSingleSpecialBubao($procData)){
            return true;
        }
        //时间段缓存数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (!isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            return false;
        }else{
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
        }
        //当前时间
        $nowTime = time();
        if ($nowTime < $timeUnitData['singleSettleApplyStartTime'] || $nowTime > $timeUnitData['singleSettleApplyEndTime'] ){
            return false;
        }
        return true;
    }
    /**
     * 结算查看权限
     * @param array $procData
     * @return boolean
     */
    function isSingleSettleCheck($procData){
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        return true;
    }
    /**
     * 结算审核权限
     * @param unknown $procData
     * @return boolean
     */
    function isSingleSettleAudit($procData){
        //时间验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //只有督导、大区总监才有审核权限
        if (!array_intersect(array('spsarea','region','area'), $this->user['userLimit'])){
            return false;
        }
        //判断是否在管理范围内
        if (!in_array($procData['storeId'], $this->userLib->getViewStoreIds())){
            return false;
        }
        //是否为特殊补报
        if ($this->isSingleSpecialBubao($procData)){
            return true;
        }
        //时间段缓存数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (!isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            return false;
        }else{
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
        }
        //当前时间
        $nowTime = time();
        if ($nowTime < $timeUnitData['singleSettleAuditStartTime'] || $nowTime > $timeUnitData['singleSettleAuditEndTime'] ){
            return false;
        }
        return true;
    }
    /**
     * 修改金额权限
     * @param array $procData
     * @return boolean
     */
    function isSingleSettleChange($procData){
        //时间验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //只有总部、地区才有金额修改权限
        if (!array_intersect(array('area','head'), $this->user['userLimit'])){
            return false;
        }
        //时间段缓存数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (!isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            return false;
        }else{
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
        }
        //当前时间
        $nowTime = time();
        if ($nowTime < $timeUnitData['singleSettleChangeStartTime'] || $nowTime > $timeUnitData['singleSettleChangeEndTime'] ){
            return false;
        }
        return true;
    }
    /**
     * 判断是否有评分权限
     * @param unknown $procData
     * @return boolean
     */
    function isSingleSettleSubScore($procData){
        //时间验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //只有督导、大区总监、地区总监才有评分权限
        if (!array_intersect(array('spsarea','region','area'), $this->user['userLimit'])){
            return false;
        }
        //判断是否在管理范围内
        if (!in_array($procData['storeId'], $this->userLib->getViewStoreIds())){
            return false;
        }
        //上级审核过，下级不允许审核
        if ($this->user['userRole'] < $procData['scoreRole']) {
            return false;
        }
        //是否为特殊补报
        if ($this->isSingleSpecialBubao($procData)){
            return true;
        }
        //取消评分时间限制
        return true;
        //时间段缓存数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (!isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            return false;
        }else{
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
        }
        //当前时间
        $nowTime = time();
        if ($nowTime < $timeUnitData['singleSettleScoreStartTime'] || $nowTime > $timeUnitData['singleSettleScoreEndTime'] ){
            return false;
        }
        return true;
    }
    /**
     * 是否具有普通补报权限
     * @param array $procData
     */
    function isSingleNormalBubao($procData){
        //年份验证
        if (!isset($procData['year']) || !$procData['year']){
            return false;
        }
        //时间验证
        if (!isset($procData['timeUnit']) || !$procData['timeUnit']){
            return false;
        }
        //查询符合条件的专营店ID集，减少数据库查询次数
        $storeIdsByTimeUnit = $this->supplementRoleModel->getValidStoreIdsByTimeUnit(1,$procData['year']);
        if (isset($storeIdsByTimeUnit[$procData['timeUnit']]) && in_array($procData['storeId'], $storeIdsByTimeUnit[$procData['timeUnit']])) {
            return true;
        }else{
            return false;
        }
        /*$findData = $this->supplementRoleModel->getData(array(
            'storeId'   =>  $this->user['userId'],
            'timeUnit'  =>  $procData['timeUnit'],
            'year'  => $procData['year'],
            'level' => 1,
            'isDel' => 0
        ));
        $nowTime = time();
        if($findData && $findData['startTime'] < $nowTime && $findData['endTime'] > $nowTime){
            return true;
        }else{
            return false;
        }*/
    }
    /**
     * 是否为特殊补报
     * @param array $procData
     */
    function isSingleSpecialBubao($procData){
        //年份验证
        if (!isset($procData['year']) || !$procData['year']){
            return false;
        }
        //时间验证
        if (!isset($procData['timeUnit']) || !$procData['timeUnit']){
            return false;
        }
        //7-8月强制为特殊补报，可以选择双月内所有时间
        /*if ($procData['timeUnit'] == 4){
            return true;
        }*/
        //查询符合条件的专营店ID集，减少数据库查询次数
        $storeIdsByTimeUnit = $this->supplementRoleModel->getValidStoreIdsByTimeUnit(2,$procData['year']);
        if (isset($storeIdsByTimeUnit[$procData['timeUnit']]) && in_array($procData['storeId'], $storeIdsByTimeUnit[$procData['timeUnit']])) {
            return true;
        }
        //时间段缓存数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if (isset($timeUnitDataList[$procData['year']][$procData['timeUnit']])){
            $timeUnitData = $timeUnitDataList[$procData['year']][$procData['timeUnit']];
            
            $nowTime = time();//当前时间戳在预算申请开始和结束时间之内
            if ($nowTime > $timeUnitData['endTime'] && $nowTime >= $timeUnitData['singleBudgetApplyStartTime'] && $nowTime <= $timeUnitData['singleBudgetApplyEndTime'] ){
                return true;
            }
        }

        return false;
    }
    /**
     * 是否有补报申请权限
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetBubaoAdd($procData){
        if ($procData['id']){
            return false;
        }
        //年份验证
        if (!isset($procData['year']) || !$procData['year']){
            return false;
        }
        //时间验证
        if (!isset($procData['timeUnit']) || !$procData['timeUnit']){
            return false;
        }
        //用户身份验证
        if ($this->user['userRole'] != $this->roleSellpoint || !array_intersect(array('apply','100CityApply'), $this->user['userLimit'])){
            return false;
        }
        //是否为普通补报
        if ($this->isSingleNormalBubao($procData)){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 是否有编辑权限
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetBubaoEdit($procData){
        //年份验证
        if (!isset($procData['year']) || !$procData['year']){
            return false;
        }
        //时间验证
        if (!isset($procData['timeUnit']) || !$procData['timeUnit']){
            return false;
        }
        //用户身份验证
        if ($this->user['userRole'] != $this->roleSellpoint || !array_intersect(array('apply','100CityApply'), $this->user['userLimit'])){
            return false;
        }
        if($procData['storeId'] != $this->user['userId']){
            return false;
        }
        //数据有效性验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //是否为普通补报
        if ($this->isSingleNormalBubao($procData)){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 是否有补报查看权限
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetBubaoCheck($procData){
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        return true;
    }
    /**
     * 是否有补报预算审核权限
     * @param array $procData
     * @return boolean
     */
    function isSingleBudgetBubaoAudit($procData){
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //只有督导、大区总监才有审核权限
        if (!array_intersect(array('spsarea','region','area'), $this->user['userLimit'])){
            return false;
        }
        //判断是否在管理范围内
        if (!in_array($procData['storeId'], $this->userLib->getViewStoreIds())){
            return false;
        }
        //是否具有补报审核权限
        if ($this->isSingleBubaoAudit($procData)) {
            return true;
        }else{
            return false;
        }
    }
    /**
     * 是否有补报结算申请权限
     * @param array $procData
     * @return boolean
     */
    function isSingleSettleBubaoEdit($procData){
        //时间验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //用户身份验证
        if ($this->user['userRole'] != $this->roleSellpoint || !array_intersect(array('apply','100CityApply'), $this->user['userLimit'])){
            return false;
        }
        if($procData['storeId'] != $this->user['userId']){
            return false;
        }
        //数据有效性验证
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //是否为特殊补报
        if ($this->isSingleNormalBubao($procData)){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 是否有补报结算查看权限
     * @param array $procData
     * @return boolean
     */
    function isSingleSettleBubaoCheck($procData){
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        return true;
    }
    /**
     * 是否有补报结算审核权限
     * @param array $procData
     * @return boolean
     */
    function isSingleSettleBubaoAudit($procData){
        if (!isset($procData['id']) || !$procData['id']){
            return false;
        }
        //只有督导、大区总监才有审核权限
        if (!array_intersect(array('spsarea','region','area'), $this->user['userLimit'])){
            return false;
        }
        //判断是否在管理范围内
        if (!in_array($procData['storeId'], $this->userLib->getViewStoreIds())){
            return false;
        }
        //是否具有补报审核权限
        if ($this->isSingleBubaoAudit($procData)) {
            return true;
        }else{
            return false;
        }
    }
    /**
     * 是否具有补报审核权限
     * @param  array  $procData
     * @return boolean
     */
    function isSingleBubaoAudit($procData){
        //是否开通审核权限
        $validAuditTimeUnitList = $this->supplementAuditModel->getValidAuditTimeUnitList();
        if (isset($validAuditTimeUnitList[$procData['year']]) && in_array($procData['timeUnit'], $validAuditTimeUnitList[$procData['year']])){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 是否具有单店普通补报开通权限
     */
    function isSingleNormalAgainLimitEdit(){
        //地区端、总部端可以开通普通补报权限
        if (!array_intersect(array('head','area'), $this->user['userLimit'])){
            return false;
        }
        return true;
    }
    /**
     * 是否具有单店特殊补报开通权限
     * @return boolean
     */
    function isSingleSpacialAgainLimitEdit(){
        //总部端可以开通特殊补报权限
        if (!array_intersect(array('head'), $this->user['userLimit'])){
            return false;
        }
        return true;
    }
    /**
     * 是否具有开通审核权限
     * @return boolean
     */
    function isAuditLimitEdit(){
        //总部端可以开通审核权限
        if (!array_intersect(array('head'), $this->user['userLimit'])){
            return false;
        }
        return true;
    }
    /**
     * 判断是否有导入管理权限
     * @return boolean
     */
    function isImport(){
        if ($this->user['userRole'] != $this->roleHead){
            return false;
        }
        return true;
    }
}